package cn.xsw.controller;


import cn.xsw.common.ResultBody;
import cn.xsw.entity.Business;
import cn.xsw.entity.query.BusinessQuery;
import cn.xsw.entity.vo.BusinessVo;
import cn.xsw.service.BusinessCategoryService;
import cn.xsw.service.BusinessService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liangYiHui
 * @since 2021-05-31
 */
@RestController
@RequestMapping("/business")
@Api(tags = "业务接口")
public class BusinessController extends BaseController{

    @Resource
    private BusinessService businessService;
    @Resource
    private BusinessCategoryService businessCategoryService;

    @ApiOperation("查询所有")
    @GetMapping("/findAll")
    public ResultBody findAll(BusinessQuery query){
        List<Business> records = businessService.list(query.wrapper());
        List<BusinessVo> businessVos = new ArrayList<>();
        records.forEach(business -> {
            BusinessVo businessVo = new BusinessVo();
            BeanUtils.copyProperties(business,businessVo);
            businessVo.setBusinessCategory(businessCategoryService.getById(business.getCid()));
            businessVos.add(businessVo);
        });
        return ResultBody.success(businessVos);
    }
    @ApiOperation("根据id查询")
    @GetMapping("/findById/{id}")
    public ResultBody findById(@PathVariable("id") int id){
        Business business = businessService.getById(id);
        BusinessVo businessVo = new BusinessVo();
        BeanUtils.copyProperties(business,businessVo);
        businessVo.setBusinessCategory(businessCategoryService.getById(business.getCid()));
        return ResultBody.success(businessVo);
    }

    @ApiOperation("分页条件查询")
    @GetMapping("/findPage")
    public ResultBody findPage(BusinessQuery query){
        Page page = businessService.page(query.page(),query.wrapper());
        List<Business> records = page.getRecords();
        List<BusinessVo> businessVos = new ArrayList<>();
        records.forEach(business -> {
            BusinessVo businessVo = new BusinessVo();
            BeanUtils.copyProperties(business,businessVo);
            businessVo.setBusinessCategory(businessCategoryService.getById(business.getCid()));
            businessVos.add(businessVo);
        });
        page.setRecords(businessVos);
        return ResultBody.success().page(page);
    }


    @ApiOperation("根据id删除")
    @GetMapping("/delById/{id}")
    public ResultBody delById(@PathVariable("id") int id){
        Boolean b = businessService.removeById(id);
        if(!b) return ResultBody.error("删除失败");
        return ResultBody.success();
    }

    @ApiOperation("添加或更新（对象id为空：添加，对象id不为空：编辑）")
    @PostMapping("/saveOrUpdate")
    public ResultBody saveOrUpdate(@RequestBody Business business){
        Boolean b = businessService.saveOrUpdate(business);
        if(!b) return ResultBody.error("操作失败");
        return ResultBody.success();
    }
}

